package com.wj.actionbar2;


import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.Window;
import android.widget.SearchView;
import android.widget.Toast;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setOverflowShowingAlways();  
	}

	//显示隐藏的菜单
	private void setOverflowShowingAlways() {
		// TODO Auto-generated method stub
		try{
		ViewConfiguration config = ViewConfiguration.get(this);
		Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");  
		menuKeyField.setAccessible(true); 
		menuKeyField.setBoolean(config, false);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	//设置菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();  
		inflater.inflate(R.menu.main, menu);
		/*如果你还希望在代码中对SearchView的属性进行配置（比如添加监听事件等），
		 * 完全没有问题，只需要在onCreateOptionsMenu()方法中获取该ActionView的实例就可以了，
		 * 代码如下所示：*/
		/*MenuInflater inflater = getMenuInflater(); 
		inflater.inflate(R.menu.main, menu); */
		/*MenuItem searchItem = menu.findItem(R.id.find);
		SearchView searchView = (SearchView) searchItem.getActionView();*/
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.it1:
			Toast.makeText(this, "球", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.it2:
			Toast.makeText(this, "伞", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.it3:
			Toast.makeText(this, "蓝牙", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.it5:
			Toast.makeText(this, "杯子", Toast.LENGTH_SHORT).show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	//菜单展开的时候调用
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		if (featureId == Window.FEATURE_ACTION_BAR && menu != null){
			try {
				//使菜单显示图标和文字
				Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible",
						Boolean.TYPE);
				m.setAccessible(true);  
				m.invoke(menu, true);  

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		return super.onMenuOpened(featureId, menu);
	}

	
	
	
}
